package com.silence.gmall.product.domain.repository.impl;

import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.silence.gmall.product.domain.entity.BaseAttrInfo;
import com.silence.gmall.product.domain.entity.BaseAttrValue;
import com.silence.gmall.product.domain.mapper.BaseAttrInfoMapper;
import com.silence.gmall.product.domain.repository.BaseAttrInfoRepository;
import com.silence.gmall.product.dto.BaseAttrInfoDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @Author silence
 * @Description 针对表【base_attr_info(属性表)】的数据库操作Repository实现
 * @Date 2025-08-17 16:28:15
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class BaseAttrInfoRepositoryImpl extends CrudRepository<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoRepository {

    /**
     * 根据分类id获取平台属性
     *
     * @param category1Id 一级分类id
     * @param category2Id 二级分类id
     * @param category3Id 三级分类id
     * @return 平台属性列表
     */
    @Override
    public List<BaseAttrInfoDto> listAttrInfoJoinAttrValue(Long category1Id, Long category2Id, Long category3Id) {
        MPJLambdaWrapper<BaseAttrInfo> wrapper = new MPJLambdaWrapper<BaseAttrInfo>()
                .selectAll(BaseAttrInfo.class)
                .selectCollection(BaseAttrValue.class, BaseAttrInfoDto::getAttrValueList)
                .leftJoin(BaseAttrValue.class, BaseAttrValue::getAttrId, BaseAttrInfo::getId)
                .and(!Objects.equals(0L, category1Id), w ->
                        w.eq(BaseAttrInfo::getCategoryId, category1Id).eq(BaseAttrInfo::getCategoryLevel, 1))
                .or(!Objects.equals(0L, category2Id), w ->
                        w.eq(BaseAttrInfo::getCategoryId, category2Id).eq(BaseAttrInfo::getCategoryLevel, 2))
                .or(!Objects.equals(0L, category3Id), w ->
                        w.eq(BaseAttrInfo::getCategoryId, category3Id).eq(BaseAttrInfo::getCategoryLevel, 3));
        return baseMapper.selectJoinList(BaseAttrInfoDto.class, wrapper);
    }
}